import { SUCCESS_CODE } from '@/core/constants/coreConstants'
import PromiseResponseHelper from '@/core/utils/PromiseResponseHelper'
import { R } from '@/components/axios/interface'

type FnType<TR> = (...args: any) => Promise<R<TR>>

type FnReturnType<T> = T extends FnType<infer R> ? R : T

class PromiseUtils {
	ofResponse<TR>(promise: Promise<R<TR>>): PromiseResponseHelper<TR> {
		return new PromiseResponseHelper<TR>(promise)
	}

	ok<T>(data: T): Promise<R<T>> {
		const res = {
			code: SUCCESS_CODE,
			data: data,
		} as R<T>

		return new Promise<R<T>>(resolve => {
			resolve(res)
		})
	}

	okWithNil<T>(): Promise<R<T>> {
		const res = {
			code: SUCCESS_CODE,
		} as R<T>

		return new Promise<R<T>>(resolve => {
			resolve(res)
		})
	}

	resolve<T>(data: T): Promise<T> {
		return new Promise<T>(resolve => {
			resolve(data)
		})
	}
}

export default new PromiseUtils()
